/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package networkrespnse;

import core.Order2;
import core.ParameterDecoder;
import utils.StringUtils;
import utils.Utils;

/**
 *
 * @author Kvest
 */
public class OrderResponse2 extends OrderResponse {
    public OrderResponse2(int id)
    {
        super(id);
    }
    
    public OrderResponse2(byte[] data)
    {
        super(ORDER_RESPONSE2);
        order = new Order2(-1);

        parse(data);
    }
    
    public void createOrder()
    {
        order = new Order2(-1);
    }
    
    public int parse(byte[] data, int offset)
    {
        byte[] buffer4 = new byte[4];  
        
        //agentPay
        System.arraycopy(data, offset, buffer4, 0, buffer4.length);
        offset += buffer4.length;
        int strSize = Utils.byteToInt(buffer4);
        byte[] buffer = new byte[strSize];
        System.arraycopy(data, offset, buffer, 0, buffer.length);
        offset += buffer.length;
        ((Order2)order).setAgentPay(StringUtils.bytesToStr(buffer));

        //features
        System.arraycopy(data, offset, buffer4, 0, buffer4.length);
        offset += buffer4.length;
        int count = Math.max(Utils.byteToInt(buffer4), 0);
        
        for (int j = 0; j < count; ++j)
        {
            System.arraycopy(data, offset, buffer4, 0, buffer4.length);
            offset += buffer4.length;
            strSize = Utils.byteToInt(buffer4);
            buffer = new byte[strSize];
            System.arraycopy(data, offset, buffer, 0, buffer.length);
            offset += buffer.length;
            ((Order2)order).addFeature(StringUtils.bytesToStr(buffer));
        }

        //agentName
        System.arraycopy(data, offset, buffer4, 0, buffer4.length);
        offset += buffer4.length;
        strSize = Utils.byteToInt(buffer4);
        buffer = new byte[strSize];
        System.arraycopy(data, offset, buffer, 0, buffer.length);
        offset += buffer.length;
        ((Order2)order).setAgentName(StringUtils.bytesToStr(buffer));

        //dispPhone
        System.arraycopy(data, offset, buffer4, 0, buffer4.length);
        offset += buffer4.length;
        strSize = Utils.byteToInt(buffer4);
        buffer = new byte[strSize];
        System.arraycopy(data, offset, buffer, 0, buffer.length);
        offset += buffer.length;
        ((Order2)order).setDispPhone(StringUtils.bytesToStr(buffer));

        //ColorClass
        System.arraycopy(data, offset, buffer4, 0, buffer4.length);
        offset += buffer4.length;
        ((Order2)order).setColorClass(Utils.byteToInt(buffer4));

        //autoClass;
        System.arraycopy(data, offset, buffer4, 0, buffer4.length);
        offset += buffer4.length;
        strSize = Utils.byteToInt(buffer4);
        buffer = new byte[strSize];
        System.arraycopy(data, offset, buffer, 0, buffer.length);
        offset += buffer.length;
        ((Order2)order).setAutoClass(StringUtils.bytesToStr(buffer));

        //searchType
//        System.arraycopy(data, offset, buffer4, 0, buffer4.length);
//        offset += buffer4.length;
//        ((Order2)order).setSearchType(Utils.byteToInt(buffer4));

        //clientName
        System.arraycopy(data, offset, buffer4, 0, buffer4.length);
        offset += buffer4.length;
        strSize = Utils.byteToInt(buffer4);

        buffer = new byte[strSize];
        System.arraycopy(data, offset, buffer, 0, buffer.length);
        offset += buffer.length;
        ((Order2)order).setClientName(StringUtils.bytesToStr(buffer));

        //receiptMustHave;
        ((Order2)order).setReceiptMustHave(data[offset++] != 0);
        
        buffer4 = null;
        buffer = null;
        
        return super.parse(data, offset);
    }
}
